﻿using System;
using Microsoft.Practices.Composite.Presentation.Events;
using Might.Infrastructure.DOM;
using Might.Infrastructure.Events;
using Might.Infrastructure.ModelicaDOM;
using Might.Infrastructure.Services;
using Might.Modelica.Parsers;

namespace Might.Core.Services {
    ///<summary>
    ///</summary>
    public class WrapperModelicaDomService : IModelicaLanguageService {
        private IProgressToken progressToken;

        /// <summary>
        /// 
        /// </summary>
        public WrapperModelicaDomService() {
            CoreServices.EventAggregator.GetEvent<FileDownloadedEvent>().Subscribe(
                FileDownloadedEventHandler, ThreadOption.UIThread, false);
        }
        
        ///<summary>
        /// odchytava udalosti typu OpenFileSystemRoot a testuje datovy naklad
        ///zda je typu ISVN
        ///pokud je otevere jej
        ///odesila udalosti o Progressu
        ///po skonceni odesle udalost OpenFileSystemRootFinished< IEventResult >
        ///ten kdo tohle odchtyva si data pretypu na napr. EventFileSystemRootResult
        ///</summary>
        ///<param name="file"></param>
        public void FileDownloadedEventHandler(IFile file) {
            ModelicaLanguageService mdlLngSrvc = new ModelicaLanguageService(CoreServices.Container, CoreServices.EventAggregator);
            try {
                mdlLngSrvc.ProcessFile(file);
            } catch (Exception excp) {
                IMightLogger logSvc = (IMightLogger) CoreServices.Container.Resolve(typeof (IMightLogger));
                logSvc.Log(this, excp.Message);
            }
        }


        public string GetSourceForModelicaClass(IModelicaClass cls) {
            string source = String.Empty;
            ModelicaLanguageService mdlLngSrvc = new ModelicaLanguageService(CoreServices.Container, CoreServices.EventAggregator);
            try {
                source = mdlLngSrvc.GetSourceForModelicaClass(cls);
            } catch (Exception excp) {
                IMightLogger logSvc = (IMightLogger) CoreServices.Container.Resolve(typeof (IMightLogger));
                logSvc.Log(this, excp.Message);
            }
            return source;
        }

        public void ProcessFile(IFile file) {
            ModelicaLanguageService mdlLngSrvc = new ModelicaLanguageService(CoreServices.Container, CoreServices.EventAggregator);
            try {
                mdlLngSrvc.ProcessFile(file);
            } catch (Exception excp) {
                IMightLogger logSvc = (IMightLogger) CoreServices.Container.Resolve(typeof (IMightLogger));
                logSvc.Log(this, excp.Message);
            }
        }
    }
}
